package com.example.logindemo2.model

import com.example.baseproject.network.AccountService
import com.example.baseproject.network.ServiceCreator
import com.example.logindemo2.model.Constant.ERR_ACCOUNT
import com.example.logindemo2.model.Constant.ERR_NETWORK
import com.example.logindemo2.model.Constant.SUC_RESULT
import com.example.logindemo2.model.Constant.SUC_RSP_CODE
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

//向后台发起请求
fun requestRequest(username: String, password: String, loginResponseResult: (Int) -> Unit) {
    val accountService = ServiceCreator.create<AccountService>()
    accountService.login(username, password).enqueue(object : Callback<LoginResponse> {
        override fun onResponse(call: Call<LoginResponse>, response: Response<LoginResponse>) {
            val responseData = response.body() ?: return
            if(responseData.responseCode == SUC_RSP_CODE) loginResponseResult(SUC_RESULT) else loginResponseResult(ERR_ACCOUNT)
        }

        override fun onFailure(call: Call<LoginResponse>, t: Throwable) {
            loginResponseResult(ERR_NETWORK)
            t.printStackTrace()
        }
    })
}